// common-units.js
Page({
  data: {
    // 页面数据
    commonUnits: {
      length: [],
      area: [],
      weight: []
    },
    unitOptions: {
      length: [
        { id: 'mm', name: '毫米' },
        { id: 'cm', name: '厘米' },
        { id: 'm', name: '米' },
        { id: 'km', name: '千米' },
        { id: 'in', name: '英寸' },
        { id: 'ft', name: '英尺' },
        { id: 'yd', name: '码' },
        { id: 'mi', name: '英里' }
      ],
      area: [
        { id: 'mm2', name: '平方毫米' },
        { id: 'cm2', name: '平方厘米' },
        { id: 'm2', name: '平方米' },
        { id: 'ha', name: '公顷' },
        { id: 'km2', name: '平方千米' },
        { id: 'in2', name: '平方英寸' },
        { id: 'ft2', name: '平方英尺' },
        { id: 'yd2', name: '平方码' },
        { id: 'ac', name: '英亩' },
        { id: 'mi2', name: '平方英里' }
      ],
      weight: [
        { id: 'mg', name: '毫克' },
        { id: 'g', name: '克' },
        { id: 'kg', name: '千克' },
        { id: 't', name: '吨' },
        { id: 'oz', name: '盎司' },
        { id: 'lb', name: '磅' }
      ]
    },
    darkMode: false
  },

  onLoad: function (options) {
    // 页面加载时执行
    console.log('common-units page onLoad');
    this.loadCommonUnits();
    // 检查夜间模式设置
    this.checkDarkMode();
    console.log('data after onLoad:', this.data);
    // 延迟输出数据，确保setData已完成
    setTimeout(() => {
      console.log('data after onLoad (delayed):', this.data);
    }, 100);
  },

  // 检查夜间模式设置
  checkDarkMode: function () {
    const app = getApp();
    const darkMode = app.globalData.darkMode;
    this.setData({
      darkMode: darkMode
    });
  },

  // 夜间模式变化回调
  onDarkModeChange: function (darkMode) {
    this.setData({
      darkMode: darkMode
    });
  },

  // 加载常用单位设置
  loadCommonUnits: function () {
    console.log('loadCommonUnits called');
    const commonUnits = wx.getStorageSync('commonUnits') || {
      length: [],
      area: [],
      weight: []
    };
    console.log('loaded commonUnits:', commonUnits);
    this.setData({
      commonUnits: commonUnits
    });
    console.log('data after loadCommonUnits:', this.data);
  },

  // 保存常用单位设置
  saveCommonUnits: function () {
    wx.setStorageSync('commonUnits', this.data.commonUnits);
    wx.showToast({
      title: '已保存',
      icon: 'success'
    });
  },

  // 切换单位选择
  toggleUnit: function (e) {
    console.log('toggleUnit called with:', e);
    const { type, unitId } = e.currentTarget.dataset;
    console.log('type:', type, 'unitId:', unitId);
    const commonUnits = JSON.parse(JSON.stringify(this.data.commonUnits)); // 深拷贝
    const unitIndex = commonUnits[type].indexOf(unitId);
    console.log('unitIndex:', unitIndex);
    
    if (unitIndex > -1) {
      // 如果已选中，则移除
      commonUnits[type].splice(unitIndex, 1);
      console.log('Removed unit:', unitId);
    } else {
      // 如果未选中，则添加
      commonUnits[type].push(unitId);
      console.log('Added unit:', unitId);
    }
    
    this.setData({
      commonUnits: commonUnits
    }, () => {
      console.log('setData callback, updated commonUnits:', this.data.commonUnits);
    });
    console.log('Updated commonUnits:', commonUnits);
  },
  
  // 页面卸载时自动保存
  onUnload: function () {
    this.saveCommonUnits();
  }
});